package cn.web.base.plugin;

import org.apache.ibatis.type.JdbcType;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.JavaTypeResolver;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.config.Context;

import java.util.List;
import java.util.Properties;


/**
 * mybatis代码生成器实体类型转换插件
 * Mybatis Geneator javaTypeResolver
 */
public class TypePlugin implements JavaTypeResolver {

    @Override
    public void addConfigurationProperties(Properties paramProperties) {
        // TODO Auto-generated method stub

    }

    @Override
    public void setContext(Context paramContext) {
        // TODO Auto-generated method stub

    }

    @Override
    public void setWarnings(List<String> paramList) {
        // TODO Auto-generated method stub

    }

    @Override
    public FullyQualifiedJavaType calculateJavaType(IntrospectedColumn paramIntrospectedColumn) {
        JdbcType jdbcType = JdbcType.forCode(paramIntrospectedColumn.getJdbcType());
        System.out.println(jdbcType.name());
        if (jdbcType.name().equals(JdbcType.DECIMAL.toString())) {
            return new FullyQualifiedJavaType("java.lang.Long");
        } else if (jdbcType.name().equals(JdbcType.VARCHAR.toString())) {
            return FullyQualifiedJavaType.getStringInstance();
        } else if (jdbcType.name().equals(JdbcType.DATE.toString())) {
            return FullyQualifiedJavaType.getDateInstance();
        } else if (jdbcType.name().equals(JdbcType.TIMESTAMP.toString())) {
            return FullyQualifiedJavaType.getDateInstance();
        } else if (jdbcType.name().equals(JdbcType.NVARCHAR.toString())) {
            return FullyQualifiedJavaType.getStringInstance();
        } else if (jdbcType.name().equals(JdbcType.DOUBLE.toString())) {
            return new FullyQualifiedJavaType("java.lang.Double");
        } else if (jdbcType.name().equals(JdbcType.FLOAT.toString())) {
            return new FullyQualifiedJavaType("java.lang.Double");
        } else if (jdbcType.name().equals(JdbcType.BLOB.toString())) {
            return new FullyQualifiedJavaType("java.sql.Blob");
        } else if (jdbcType.name().equals(JdbcType.CLOB.toString())) {
            return new FullyQualifiedJavaType("java.sql.Clob");
        } else {
            return FullyQualifiedJavaType.getStringInstance();
        }
    }

    @Override
    public String calculateJdbcTypeName(IntrospectedColumn paramIntrospectedColumn) {
        int code = paramIntrospectedColumn.getJdbcType();
        if (code == 3) {
            return JdbcType.NUMERIC.name();
        }
        return JdbcType.forCode(code).name();
    }

}
